//
//  SNCheckVc.swift
//  project
//
//  Created by 黄建华 on 2018/2/23.
//  Copyright © 2018年 黄建华. All rights reserved.
//

import UIKit
class SNCheckVc: SNBaseSubViewController {
        let checktime = 60
        let  selectView = SNHealthySelectTypeView.init(frame:SMakeRect(x1: 0, y1:ValueIXOrOther(ip: 74, ix: 94), width1: ScreenWitdh, height1: 33))
        let flashView = SNFlashView(frame:SMakeRect(x1: 0, y1: 0, width1: 180, height1: 180))
        var timer:Timer!
        var checkImageView = CreatUIImageView(frame: SMakeRect(x1: 0, y1: ScreenHeight-110, width1: ScreenWitdh*2, height1: 100), imageName: "检测动画2")
        var showinfoView = UIView()
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector:#selector(timerRun), userInfo: nil, repeats: true)
        SN_PublicClassSwift?.checkType = 1;
        self.view.addSubview(selectView)

        let backButton = CreatUIButton(frame: SMakeRect(x1: 5, y1: ValueIXOrOther(ip: 20, ix: 40), width1: 44, height1: 44), text: "")
        backButton.setImage(UIImage(named:"进入"), for:.normal)
        backButton.addTarget(self, action: #selector(backView), for: .touchUpInside)
        self.view.addSubview(backButton)
        
        let titleLable = CreatUIlabel(frame: SMakeRect(x1: 0, y1: ValueIXOrOther(ip: 20, ix: 40), width1: ScreenWitdh, height1: 44), text:SMakeText(key: "我的体检"))
        titleLable.font = UIFont.boldSystemFont(ofSize: 17)
        self.view.addSubview(titleLable)
        
//        let line = UIView(frame:SMakeRect(x1: 0, y1: ValueIXOrOther(ip: 64, ix: 84), width1: ScreenWitdh, height1: 2))
//        line.backgroundColor = UIColor.lightGray
//        self.view.addSubview(line)
        let line = SNShadowView(frame:SMakeRect(x1: 0, y1: ValueIXOrOther(ip: 64, ix: 84), width1: ScreenWitdh, height1: 2))
        line.show(withType: 1)
        self.view.addSubview(line)
        
        flashView.center = SmakePoint(x1: self.view.center.x, y1: self.view.center.y - 72)
        self.view.addSubview(flashView)
        
        weak var weakwelf=self
        selectView.selectType = { (index:NSInteger)in
            weakwelf?.selectType(type: index)
            if SN_PublicClassSwift?.checkType==1 {
                weakwelf?.flashView.valueLabel.text = "-- \n bpm"
            }
            if SN_PublicClassSwift?.checkType==2 {
                weakwelf?.flashView.valueLabel.text = "-- \n %"
            }
            if SN_PublicClassSwift?.checkType==3{
                weakwelf?.flashView.valueLabel.text = "-- \n mmHg"
            }
        }
        SN_PublicClassSwift?.checkBlock = { (value:NSInteger) -> ()in
            if value==1
            {
             weakwelf?.selectView.isCheck = true
             weakwelf?.checkImageView.frame = SMakeRect(x1: 0, y1: ScreenHeight-110, width1: ScreenWitdh*2, height1: 90)
            }
            else
            {
             weakwelf?.selectView.isCheck = false
            }
        }
        self.view.addSubview(checkImageView)
        showinfoView = makeInfoView(frame: SMakeRect(x1: 0, y1: 0, width1: 220, height1: 160), info: SMakeText(key: "是否停止体检?"))
        
        NotificationCenter.default.addObserver(self, selector: #selector(self.getNotice), name: NSNotification.Name(rawValue: "体检页面通知"), object: nil)
    }
    
    @objc func getNotice(notice: NSNotification){
        if selectView.isCheck == true
        {
            showinfoView.popupWithtype(PopupViewOption_colorLump, succeed: { (succeeView:UIView?) in
                
            }) { (v2:UIView?) in
                
            }
        }
    }
    
    @objc func okButtonClick ()
    {
        flashView.EndAnimation()
        showinfoView.dismissPopup()
        checkImageView.frame = SMakeRect(x1: 0, y1: ScreenHeight-110, width1: ScreenWitdh*2, height1: 90)
        selectView.isCheck = false
    }
    
    @objc func canButonClick ()
    {
        showinfoView.dismissPopup()
    }
    
    @objc func timerRun ()
    {
        if (selectView.isCheck == true)
        {
            let width = ScreenWitdh/10.0
            if checkImageView.frame.origin.x  >= -ScreenWitdh + width
            {
                checkImageView.frame = SMakeRect(x1: checkImageView.frame.origin.x - width, y1: ScreenHeight-110, width1: ScreenWitdh*2, height1: 90)
            }
            else
            {
                checkImageView.frame = SMakeRect(x1: 0, y1: ScreenHeight-110, width1: ScreenWitdh*2, height1: 90)
            }
        }
    }
    
    @objc func backView ()
    {
        if flashView.starting {
            ShowMBProgressHUD(title: SMakeText(key: "正在体检"), time: 2.0)
            return
        }
        self.navigationController?.popViewController(animated: true)
    }
    
    @available(iOS 9.0, *)
    @objc func shareClick ()
    {
    if flashView.starting {
        ShowMBProgressHUD(title: SMakeText(key: "正在体检"), time: 2.0)
        return
    }
        
    let image = screenSnapshot(save: true)
    let items = [image]
        let activityViewController = UIActivityViewController.init(activityItems: items , applicationActivities: nil)
    let excludedActivityTypes = [ UIActivityType.message, UIActivityType.mail, UIActivityType.print, UIActivityType.copyToPasteboard, UIActivityType.assignToContact, UIActivityType.saveToCameraRoll, UIActivityType.addToReadingList, UIActivityType.postToFlickr, UIActivityType.postToVimeo, UIActivityType.postToTencentWeibo, UIActivityType.airDrop,
                                          UIActivityType.openInIBooks]
        activityViewController.excludedActivityTypes=excludedActivityTypes
        self.present(activityViewController,animated: true,completion: nil)
    }
    
    func screenSnapshot(save: Bool) -> UIImage? {
        guard let window = UIApplication.shared.keyWindow else { return nil }
        // 用下面这行而不是UIGraphicsBeginImageContext()，因为前者支持Retina
        UIGraphicsBeginImageContextWithOptions(window.bounds.size, false, 0.0)
        window.layer.render(in: UIGraphicsGetCurrentContext()!)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        if save { UIImageWriteToSavedPhotosAlbum(image!, self, nil, nil) }
        return image
    }
    func  CreatUIlabel (frame:CGRect,text:String) ->UILabel
    {
        let label = UILabel(frame:frame)
        label.adjustsFontSizeToFitWidth = true
        label.textColor = UIColor.black
        label.text = text
        label.font = SMakeFont(x: 17.0)
        label.textAlignment = .center
        return label
    }
    func selectType (type:NSInteger)
    {
        SN_PublicClassSwift?.checkType = type
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    override func  touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        if selectView.isCheck == true
        {
            showinfoView.popupWithtype(PopupViewOption_colorLump, succeed: { (succeeView:UIView?) in
                
            }) { (v2:UIView?) in
                
            }
        }
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(true)
        // 销毁通知
        NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: "体检页面通知"), object: nil)
    }
    
    func makeInfoView (frame:CGRect,info:String) ->UIView
    {
        let infoView = UIView(frame:frame)
        infoView.backgroundColor = .white
        infoView.layer.borderWidth = 1
        infoView.layer.borderColor = UIColor.black.cgColor
        infoView.layer.cornerRadius = 6.0
        infoView.layer.masksToBounds = true
        
        let infoLabel = CreatUIlabel(frame: SMakeRect(x1: 0, y1: infoView.height/4, width1: infoView.width, height1: 60), text: info)
        infoLabel.numberOfLines = 2
        infoView.addSubview(infoLabel)
        
        
        let line = CreatUIlabel(frame: SMakeRect(x1: 0, y1: infoView.height - 40, width1: infoView.width, height1: 1), text:"info")
        line.backgroundColor = .black
        infoView.addSubview(line)
        
        let line2 = CreatUIlabel(frame: SMakeRect(x1: infoView.width/2, y1: infoView.height-40, width1: 1, height1:40), text:"info")
        line2.backgroundColor = .black
        infoView.addSubview(line2)
        
        let okButton = CreatUIButton(frame: SMakeRect(x1: 0, y1: infoView.height - 40, width1: infoView.width/2, height1: 40), text: SMakeText(key: "确定"))
        okButton.setTitleColor(.black, for: .normal)
        
        let cancelButton = CreatUIButton(frame: SMakeRect(x1: infoView.width/2, y1: infoView.height - 40, width1: infoView.width/2, height1: 40), text: SMakeText(key: "取消"))
        cancelButton.setTitleColor(.black, for: .normal)
        infoView.addSubview(okButton)
        infoView.addSubview(cancelButton)
        
        okButton.addTarget(self, action: #selector(okButtonClick), for: .touchUpInside)
        cancelButton.addTarget(self, action:#selector(canButonClick), for: .touchUpInside)
        
        return infoView
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */
}
